#include "skiprulesmodel.h"

SkipRulesModel::SkipRulesModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

void SkipRulesModel::setList(QList< QPair<QRegExp, bool> > *skipRulesList){
    m_skipRulesList = skipRulesList;
}

int SkipRulesModel::rowCount(const QModelIndex &/*parent*/) const {
    return m_skipRulesList->count();
}

int SkipRulesModel::columnCount(const QModelIndex &/*parent*/) const {
    return 3;
}

QVariant SkipRulesModel::data(const QModelIndex &index, int role) const {
    if(!m_skipRulesList)
        return QVariant();
    const QList< QPair<QRegExp, bool> > & list = *m_skipRulesList;
    if(!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole){
        int r = index.row();
        int c = index.column();
        if(c==0){
            if(list[r].second)
                return QString("Yes");
            else
                return QString("No");
        }
        else if(c==1){
            if(list[r].first.patternSyntax()==QRegExp::FixedString)
                return QString("FixedString");
            else if(list[r].first.patternSyntax()==QRegExp::Wildcard)
                return QString("Wildcard");
            else if(list[r].first.patternSyntax()==QRegExp::WildcardUnix)
                return QString("WildcardUnix");
            else if(list[r].first.patternSyntax()==QRegExp::RegExp)
                return QString("RegExp");
            else if(list[r].first.patternSyntax()==QRegExp::RegExp2)
                return QString("RegExp2");
        }
        else if(c==2){
            return list[r].first.pattern();
        }
        else{
            return QVariant();
        }
    }
    return QVariant();
}

bool SkipRulesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if(!m_skipRulesList)
        return false;
    QList< QPair<QRegExp, bool> > & list = *m_skipRulesList;
    if(index.isValid() && (role==Qt::EditRole)){
        int r = index.row();
        int c = index.column();
        QString str = value.toString();
        switch(c){
        case 0:
            if(str=="Yes")
                list[r].second = true;
            else
                list[r].second = false;
            return true;
            break;
        case 1:
            if(str=="FixedString")
                list[r].first.setPatternSyntax(QRegExp::FixedString);
            else if(str=="Wildcard")
                list[r].first.setPatternSyntax(QRegExp::Wildcard);
            else if(str=="WildcardUnix")
                list[r].first.setPatternSyntax(QRegExp::WildcardUnix);
            else if(str=="RegExp")
                list[r].first.setPatternSyntax(QRegExp::RegExp);
            else if(str=="RegExp2")
                list[r].first.setPatternSyntax(QRegExp::RegExp2);
            return true;
            break;
        case 2:
           list[r].first.setPattern(value.toString());
           return true;
           break;
        default:return false;
           break;
        }
    }
    return false;
}

QVariant SkipRulesModel::headerData(int section,Qt::Orientation orientation, int role) const {
    if(role== Qt::DisplayRole){
        if(orientation == Qt::Horizontal){
            switch(section){
                case 0:return tr("Match");
                       break;
                case 1:return tr("Syntax");
                       break;
                case 2:return tr("Pattern");
                       break;
                default:return QVariant();
                       break;
            }
        }
        else {
            return section;
        }
    }
    return QVariant();
}

Qt::ItemFlags SkipRulesModel::flags(const QModelIndex &index) const {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    return flags|Qt::ItemIsEditable;
}

void SkipRulesModel::update(){
    reset();
}

bool SkipRulesModel::insertRows(int row, int count, const QModelIndex& parent){
    beginInsertRows(parent,row,row+count-1);
    QPair<QRegExp,bool> pair;
    pair.first.setPatternSyntax(QRegExp::Wildcard);
    pair.second = true;
    for(int i = 0; i < count; ++i){
        m_skipRulesList->insert(row+i,pair);
    }
    endInsertRows();
    return true;
}

bool SkipRulesModel::removeRows(int row, int count, const QModelIndex& parent){
    if(row+count-1 > m_skipRulesList->count())
        return false;
    beginRemoveRows(parent,row,row+count-1);
    for(int i = 0; i < count; ++i){
        m_skipRulesList->removeAt(row);
    }
    endRemoveRows();
    return true;
}
